/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlayerCreatePanel.java
 *
 * Created on Jul 29, 2011, 2:09:47 PM
 */
package deatax.display.gui;

import deatax.network.ChangedListener;
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JColorChooser;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author pson
 */
public class PlayerCreatePanel extends javax.swing.JPanel implements Runnable, DocumentListener {

    
    public static Color[] defaults = new Color[]{Color.BLUE, Color.RED, Color.GREEN, Color.CYAN, Color.MAGENTA, Color.YELLOW, Color.ORANGE, Color.GRAY};
    private int num;
    private boolean editable;
    ArrayList<ChangedListener> listeners;
    DelayThread thread;

    /** Creates new form PlayerCreatePanel */
    public PlayerCreatePanel(int n) {
        initComponents();
        num = n;
        if (n < defaults.length) {
            colorPick.setBackground(defaults[n]);
        }
        listeners = new ArrayList<ChangedListener>();
        nameField.setEmptyText("Player " + n + " Name");
        editable = true;
        nameField.getDocument().addDocumentListener(this);
        thread = null;
    }

    public int getN() {
        return num;
    }

    public void addListener(ChangedListener listener) {
        listeners.add(listener);
    }

    public void removeListener(ChangedListener listener) {
        listeners.remove(listener);
    }

    public void clearListeners() {
        listeners.clear();
    }

    public void fireListeners() {
        for (ChangedListener changedListener : listeners) {
            changedListener.changed(nameField.getText(), colorPick.getBackground());
        }
    }

    public String getRawName() {
        return nameField.getText();
    }
    
    public void setPlayerName(String name) {
        nameField.getDocument().removeDocumentListener(this);
        nameField.setText(name);
        nameField.getDocument().addDocumentListener(this);
    }
    
    public String getPlayerName() {
        if (nameField.getText().isEmpty()) {
            return "Player " + num;
        }
        return nameField.getText();
    }

    public void setEditable(boolean b) {
        editable = b;
        nameField.setEditable(b);
    }

    public Color getColor() {
        return colorPick.getBackground();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nameField = new deatax.display.gui.JLabeledTextField();
        colorPick = new javax.swing.JPanel();

        nameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameFieldActionPerformed(evt);
            }
        });

        colorPick.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                colorPickMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout colorPickLayout = new javax.swing.GroupLayout(colorPick);
        colorPick.setLayout(colorPickLayout);
        colorPickLayout.setHorizontalGroup(
            colorPickLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );
        colorPickLayout.setVerticalGroup(
            colorPickLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(nameField, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(colorPick, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(colorPick, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(nameField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void colorPickMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_colorPickMouseClicked
        if (editable) {
            Color c = JColorChooser.showDialog(this, "Choose Colour", colorPick.getBackground());
            if (c != null) {
                colorPick.setBackground(c);
                fireListeners();
            }
        }

    }//GEN-LAST:event_colorPickMouseClicked

    private void nameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameFieldActionPerformed
        fireListeners();
    }//GEN-LAST:event_nameFieldActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel colorPick;
    private deatax.display.gui.JLabeledTextField nameField;
    // End of variables declaration//GEN-END:variables

    void setColour(Color color) {
        colorPick.setBackground(color);
    }
    
    public void delayUpdate() {
        if (thread != null) {
            thread.kill();
        }
        thread = new DelayThread(300, this);
        thread.start();
    }

    @Override
    public void run() {
        fireListeners();
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        delayUpdate();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        delayUpdate();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        
    }
    
    
}
